package net.borderwars.util;

import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author ehubbard
 *         Date: Aug 5, 2005
 *         Time: 10:26:52 AM
 */
public class SparseArray<Type> {
    private SortedMap<Integer, Type> data = null;

    public SparseArray () {
        data = new TreeMap<Integer, Type> ();
    }

    private SparseArray (SortedMap<Integer, Type> data) {
        this.data = data;
    }

    public SparseArray getSubArray (int a, int b) {
        SortedMap<Integer, Type> sm = data.subMap (a, b);
        SparseArray<Type> sa = new SparseArray<Type> (sm);
        return (sa);
    }

    public void put (int index, Type elemenet) {
        data.put (index, elemenet);
    }

    public Type get (int index) {
        if (data.containsKey (index)) {
            return (data.get (index));
        }
        return (null);
    }

    public int getNonEmptyElementSize () {
        return (data.size ());
    }
}
